import Vue from 'vue';
import App from './App';
import VueTouchRipple from 'vue-touch-ripple';
import 'vue-touch-ripple/dist/vue-touch-ripple.css';
import ImageViewer from 'image-viewer-gallery';

import Vant, { Toast, Notify } from 'vant';
// import { Toast } from 'vant';

import 'vant/lib/index.css';

// 支持IE 浏览器
import 'babel-polyfill';
// 数据存储
import db from './db';
// 路由配置
import router from './router';
// 支持$http
import axios from 'axios';
import filters from './filters';
import FAKEDATA from './假数据';
import util from './util';
import store from './store';

var mLingwu = window.mLingwu;

Vue.config.devtools = true;
Vue.use(VueTouchRipple, {
  color: '#093568',
  opacity: 0.3,
  speed: 3,
  transition: 'ease'
});
Vue.use(Vant);
Vue.use(Toast, Notify);
Vue.prototype.$http = axios;
Vue.prototype.$history = [];
Vue.config.productionTip = false;
Vue.prototype.$db = db;
Vue.prototype.$util = util;
Vue.prototype.$ImageViewer = ImageViewer;

Vue.prototype.getOperator = function () {
  return {
    lastoperatetime: new Date().format('yyyy-MM-dd hh:mm:ss'),
    lastoperator: db.get('username'),
    userid: db.get('userid')
  };
};

var username = '';
var userid = '';
var accessToken = '';

if (process.env.NODE_ENV == 'development' || util.getQueryString('debug')) {
  mLingwu = {
    printFile: (fileUrl, type) => {
      alert('文件打印', fileUrl, type);
    }
  };
  accessToken = 'a300311a410d47b9a1f3ed2a6fc77c56';
  username = '章正伟';
  userid = 'tanijlbpu5vq';
} else {
  accessToken = mLingwu.getToken();
  username = util.getQueryString('username');
  userid = mLingwu.getUserid();
}
if (accessToken && username) {
  db.set('accessToken', accessToken);
  db.set('username', username);
  db.set('userid', userid);
} else {
  Notify({ type: 'danger', message: '无有效的凭证信息，无法访问' });
  router.replace({path: '/'});
}
Vue.prototype.$mLingwu = mLingwu;

Object.defineProperty(Image.prototype, 'authsrc', {
  writable: true,
  enumerable: true,
  configurable: true
});

// 统一请求方法
Vue.prototype.$getAndLoad = function (chartName, apiUrl, callback, ...params) {
  // process.env.NODE_ENV != 'development' && (apiUrl = 'http://183.207.196.25:50087/ga/jygawebcitycrisis/' + apiUrl);
  var keys = Object.keys(FAKEDATA);
  var fakeApi = '';
  keys.forEach(key => {
    if (apiUrl.includes(key)) {
      fakeApi = key;
    }
  });
  if (fakeApi) {
    setTimeout(() => {
      callback(FAKEDATA[fakeApi].data);
    }, 200);
    return;
  }
  axios(apiUrl, {
    // headers: {
    //   requestType: 'app',
    //   applyID: '95b71316cebe4a18906066d9799faa59',
    //   secretKey: '13FFDB8B49D6568D78EB907AFBB6C17D',
    //   accessToken: db.get('accessToken')
    // }
  }).then(function (res) {
    if (res.data) {
      if (Array.isArray(res.data)) {
        callback(res.data[0], params);
      } else {
        callback(res.data, params);
      }
    } else {
      Toast({ message: `${chartName}获取失败`, position: 'bottom' });
    }
  }, function () {
    // eslint-disable-next-line 
    callback({code: '0', msg: '请求失败'});
  });
};

axios.defaults.withCredentials = true;
axios.interceptors.request.use(config => {
  return config;
}, err => {
  Vue.$vux.toast.text('请求超时');
  return Promise.resolve(err);
});
axios.interceptors.response.use(data => {
  return data;
}, err => {
  Vue.$vux.toast.text('服务器异常');
  return Promise.resolve(err);
});
for (let key in filters) {
  Vue.filter(key, filters[key]);
}

router.beforeResolve((to, from, next) => {
  if (to.meta.auth) {
    //
  }
  next();
});

router.fail = function (msg) {
  router.push({name: 'Fail',
  params: {
    msg: msg
  }});
};

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
});
